package com.fjec.wx.controller;

import com.fjec.common.Const;
import com.fjec.common.wx.WxAccessTokenHelper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
public class AccessTokenController {

    @Resource
    private WxAccessTokenHelper wxAccessTokenHelper;

    /**
     * 获取 wx access token
     *
     * @return
     */
    @GetMapping("/get/access/token")
    public String getAccessToken() {
        String result = null;
        try {
            result = wxAccessTokenHelper.getAccessToken();
        } catch (Exception ex) {
            MDC.put(Const.TRACE_ID_KEY, "ACCESS_TOKEN");
            log.error(ex.getMessage(), ex);
            MDC.remove(Const.TRACE_ID_KEY);
        }
        return result;
    }

    /**
     * 重新缓存wx saccess token
     *
     * @return
     */
    @GetMapping("/refresh/access/token")
    public String refreshAccessToken() {
        String result = null;
        try {
            result = wxAccessTokenHelper.cacheAccessToken();
        } catch (Exception ex) {
            MDC.put(Const.TRACE_ID_KEY, "ACCESS_TOKEN");
            log.error(ex.getMessage(), ex);
            MDC.remove(Const.TRACE_ID_KEY);
        }
        return result;
    }

    /**
     * 清除当前缓存
     *
     * @return
     */
    @GetMapping("/remove/access/token")
    public void removeAccessToken() {
        wxAccessTokenHelper.removeAccessToken();
    }

}
